package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.TDataFromPo;
import com.csii.adhoc.dataready.mapper.TDataFromMapper;
import com.csii.adhoc.dataready.service.TDataFromService;
import com.csii.adhoc.exception.ParamValidityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 数据集接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tDataFromService")
@Slf4j
public class TDataFromServiceImpl implements TDataFromService  {

    @Autowired
    TDataFromMapper tDataFromMapper;

    @Override
    @Transactional
    public void tDataFromMaintain(TDataFromPo tDataFromPo, String optMode) {
        //检查上传的值

        switch (optMode) {
            case TDataConstant.OPT_MODE_ADD:
                insertOneTDataFrom(tDataFromPo);
                break;
            case TDataConstant.OPT_MODE_UPDATE:
                updateTDataFromByPrimaryKey(tDataFromPo);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteTDataFromByPrimaryKey(tDataFromPo.getTableId());
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
        }
    }

    @Transactional
    @Override
    public void insertOneTDataFrom(TDataFromPo tDataFromPo) {
        if (StringUtils.isEmpty(tDataFromPo.getTableId())) {
            tDataFromPo.setTableId(UUID.randomUUID().toString().replace("-", ""));
        }
        tDataFromPo.setAlias(null);
        tDataFromMapper.insertOneTDataFrom(tDataFromPo);
    }

    @Transactional
    @Override
    public int updateTDataFromByPrimaryKey(TDataFromPo tDataFromPo) {
        return tDataFromMapper.updateByPrimaryKey(tDataFromPo);
    }

    @Transactional
    @Override
    public int deleteTDataFromByPrimaryKey(String ModelId) {
        return tDataFromMapper.deleteByPrimaryKey(ModelId);
    }

    @Override
    public List<TDataFromPo> selectListTDataFrom(List list) {
        return tDataFromMapper.selectListTDataFrom(list);
    }
    @Override
    public List<TDataFromPo> selectListTDataFrom(String modelId) {
        return tDataFromMapper.selectListTDataFrom(modelId);
    }

    @Override
    public TDataFromPo selectOneTDataFrom(String tableId) {
        return tDataFromMapper.selectOneTDataFrom(tableId);
    }
    @Override
    public Map selCustDataDbTable(String sql){
        return tDataFromMapper.selCustDataDbTable(sql);
    }
}
